package  
{
	import flash.geom.Point;
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.FlxBar;
	
	/**
	 * ...
	 * @author Enric Llagostera
	 */
	public class SplashPlayer extends FlxSprite 
	{
		[Embed(source = "../assets/SplashPlayerTest.png")] public const imgSplashPlayer:Class;
		[Embed(source = "../assets/SplashPlayerTestYellow.png")] public const imgSplashPlayerYellow:Class;
		[Embed(source = "../assets/SplashPlayerTestPurple.png")] public const imgSplashPlayerPurple:Class;
		public var team:int = 0;
		
		private var emitter:FlxEmitter;
		private var moveAccel:Number = 400;
		private var splashSpeed:Number = 20;
		private var splashSpray:Number = 150;
		private var splashDirection:Point = new Point(0, 0);
		private var splashDirectionDestination:Point = new Point(0, 0);
		
		public var sprayMeter:Number = 4;
		private var sprayRechargeRate:Number = 3;
		private var sprayUseRate:Number = 2;
		public var sprayMaxMeter:Number = sprayMeter;
		private var sprayLifespan:Number = 0.3;
		
		
		private var keyUp:String;
		private var keyDown:String;
		private var keyLeft:String;
		private var keyRight:String;
		private var keySplash:String;
		
		public var sprayBar:FlxBar;
		
		
		private var sound1:FlxSound;
		private var sound2:FlxSound;
		private var sound3:FlxSound;
		private var sound4:FlxSound;
		
		public function SplashPlayer(pX:Number, pY:Number, pTeam:int,  keyUp:String, keyDown:String, keyLeft:String, keyRight:String, keySplash:String, isPurple:Boolean) 
		{
			super(pX, pY);
			if (isPurple)
			{
				loadGraphic(imgSplashPlayerPurple, false, true);
			}
			else
			{
				loadGraphic(imgSplashPlayerYellow, false, true);
			}
			
			
			
			ID = team = pTeam;
			
			chargeEmitter();
			
			drag.x = drag.y = 400;
			moveAccel 		= 400;
			maxVelocity.x = maxVelocity.y = 140;
			
			width = height = 22;
			offset.x = offset.y = 4;
			centerOffsets(false);
			
			
			this.keyUp = keyUp;
			this.keyDown = keyDown;
			this.keyLeft = keyLeft;
			this.keyRight = keyRight;
			this.keySplash = keySplash;
			
			FlxG.watch(this, "sprayMeter");
			
			sprayBar = new FlxBar(pX, pY - 5, FlxBar.FILL_LEFT_TO_RIGHT, 30, 4, this, "sprayMeter", 0, sprayMaxMeter, false);
			
			if (team == 1)
				sprayBar.createFilledBar(0xff000000, 0xffffff00, true, 0xff000000);
			else
				sprayBar.createFilledBar(0xff000000, 0xff6b3d99, true, 0xff000000);
				
			Registry.groupUI.add(sprayBar);
			
			sound1 = new FlxSound();
			sound1.loadEmbedded(Registry.hose1snd, true);
			sound1.play();
			sound2 = new FlxSound();
			sound2.loadEmbedded(Registry.hose2snd, true);
			sound2.play();
			sound3 = new FlxSound();
			sound3.loadEmbedded(Registry.hose3snd, true);
			sound3.play();
			sound4 = new FlxSound();
			sound4.loadEmbedded(Registry.hose4snd, true);			
			sound4.play();
			pauseAll();
		}
		
		
		
		override public function update():void 
		{
			super.update();
			
			if (velocity.x > 0) facing = LEFT;
			else if (velocity.x < 0) facing = RIGHT;
			
			acceleration.x = acceleration.y = 0;
			// Movement controls
			ProcessMovement();
			
			// Deals with the spray recharge
			processRecharge();
			
			// Other actions
			ProcessSplash();
			
			
			
			/*  -- > Old velocity code
			if (velocity.x != 0 || velocity.y != 0)
			{
				var velocityAngle:Number = Math.atan2(velocity.y, velocity.x);
				splashDirection = new Point(Math.cos(velocityAngle) * splashSpeed, Math.sin(velocityAngle) * splashSpeed);
			}
			*/
			
			if (splashDirectionDestination.x != 0 || splashDirectionDestination.y != 0)
			{
				var velocityAngle:Number = Math.atan2(splashDirectionDestination.y, splashDirectionDestination.x);
				splashDirection = new Point(Math.cos(velocityAngle) * splashSpeed, Math.sin(velocityAngle) * splashSpeed);
			}			
			
			emitter.x = x + width/2 + splashDirection.x;
			emitter.y = y + height/2 + splashDirection.y;
		}
		
		private function processRecharge():void {
			
			if(!FlxG.keys.pressed(keySplash))
				sprayMeter += FlxG.elapsed * sprayRechargeRate;
			
			if (sprayMeter >= sprayMaxMeter)
				sprayMeter = sprayMaxMeter;
			
		}
		
		private function chargeEmitter():void
		{
			emitter = new FlxEmitter(this.x, this.y, 50);
			emitter.setRotation(0, 0);
			for (var i:int = 0; i < emitter.maxSize; i++)
			{
				var p:SplashParticle = new SplashParticle(this,this.team == 1);
				var p:SplashParticle;
				if (this.team == 1)	p = new SplashParticle(this, true);
				else p = new SplashParticle(this, false);
				
				emitter.add(p);
				
				p.scale.x *= FlxG.random() + 0.2;
				
				if (p.scale.x >= 0.8) p.scale.x = 0.85;
				
				p.scale.y = p.scale.x;
				
				
				p.width *= p.scale.x;
				p.height *= p.scale.x;
				p.centerOffsets(true);
				Registry.groupSplash.add(p);
			}
			
			FlxG.state.add(emitter);
		}
		
		
		private function ProcessSplash():void
		{
			
			if (FlxG.keys.pressed(keySplash)) {
				maxVelocity.x = maxVelocity.y = 80;
			}
			else {
				maxVelocity.x = maxVelocity.y = 140;// maxVelocity.y * 2;
			}
			
			if (FlxG.keys.pressed(keySplash) && sprayMeter > 0)
			{
				if (FlxG.keys.justPressed(keySplash))
				{
					switch(int(Math.random() * 4))
					{
						case 0:
							sound1.play();
							break;
						case 1:
							sound2.play();
							break;
						case 2:
							sound3.play();
							break;
						case 3:
							sound4.play();
							break;							
					}
				}
				
				var modX:Number = (FlxG.random()-0.5)*splashSpray;
				var modY:Number = (FlxG.random()-0.5)*splashSpray;
				emitter.setXSpeed(splashDirection.x*splashSpeed+modX,splashDirection.x*splashSpeed+modX);
				emitter.setYSpeed(splashDirection.y*splashSpeed+modY,splashDirection.y*splashSpeed+modY);
				emitter.start(true, sprayLifespan, 0, 1);
				sprayMeter -= sprayUseRate * FlxG.elapsed;
				if (sprayMeter <= 0) sprayMeter = 0;
			}
			
			if (FlxG.keys.justReleased(keySplash))
			{
				pauseAll();
			}
			
		}
		
		private function pauseAll():void
		{
			sound1.stop();
			sound2.stop();
			sound3.stop();
			sound4.stop()
		}
		
		private function ProcessMovement():void
		{
			if (FlxG.state is PlayState && (FlxG.state as PlayState).gameSTarte)
			{
				// movement
				if (FlxG.keys.pressed(keyUp)) {
					
					if (velocity.y > 0) velocity.y = 0;
					acceleration.y = -moveAccel;
					splashDirectionDestination.y = -1;
				}
				if (FlxG.keys.pressed(keyRight)) {
					
					if (velocity.x < 0) velocity.x = 0;
					acceleration.x = moveAccel;
					splashDirectionDestination.x = 1;
				}
				
				if (FlxG.keys.pressed(keyDown)) {
					
					if (velocity.y < 0) velocity.y = 0;
					acceleration.y = moveAccel;
					splashDirectionDestination.y = 1;
				}
				
				if (FlxG.keys.pressed(keyLeft)) {
					
					if (velocity.x > 0) velocity.x = 0;
					acceleration.x = -moveAccel;
					splashDirectionDestination.x = -1;
				}
				
				if (!FlxG.keys.pressed(keyLeft) && !FlxG.keys.pressed(keyRight))
				{
					splashDirectionDestination.x = 0;
				}
				if (!FlxG.keys.pressed(keyUp) && !FlxG.keys.pressed(keyDown))
				{
					splashDirectionDestination.y = 0;
				}
			}
			
		}
		
	}

}